<?php
	require_once("../inc/constants.inc.php");
	require_once("../inc/connection.php");
	session_start();
	if(isset($_POST))
	{
		$question = mysql_real_escape_string($_POST['questionTextArea']);			
		$optionOne = mysql_real_escape_string($_POST['optionOne']);
		$optionTwo = mysql_real_escape_string($_POST['optionTwo']);
		$optionThree = mysql_real_escape_string($_POST['optionThree']);
		$optionFour = mysql_real_escape_string($_POST['optionFour']);
		$questionNumber = $_POST['hiddenQuestionId'];
		$correctOption = $_POST['optionRadio'];
	
	switch ($correctOption) {
		case "a":
			$correctAnswer = $optionOne;
			break;
		case "b":
			$correctAnswer = $optionTwo;
			break;
		case "c":
			$correctAnswer = $optionThree;
			break;
		case "d":
			$correctAnswer = $optionFour;
			break;
	}
	
		$test_type = mysql_real_escape_string($_POST['test_type']); // this should come as a hidden parameter
	}
	$questionTable = "question_table";

	if(isset($_POST['add']))
	{
		//validateQuestion($questionTable, $question, $optionOne, $optionTwo, $optionThree, $optionFour, $correctOption, $questionNumber);
	
		$sql = "insert into `question_table` (`typeId`, `question`, `correctAnswer`, `correctOption`, `optionOne`, `optionTwo`, `optionThree`, `optionFour`) values ('$test_type','$question','$correctAnswer','$correctOption','$optionOne','$optionTwo', '$optionThree', '$optionFour')";
		if(mysql_query($sql))
		{
			$url="../add-update.php?isAdded=yes&test_type=$test_type&action=add";
			header("Refresh:0;URL=$url");
		}
		else
		{
			$url="../add-update.php?isAdded=no&action=add";
			header("Refresh:0;URL=$url");
		}
		
	}
	if(isset($_POST['update']))
	{
		$q = "UPDATE `$questionTable` SET typeId='$test_type', question='$question', correctAnswer='$correctAnswer', correctOption='$correctOption', optionOne='$optionOne', optionTwo='$optionTwo', optionThree='$optionThree', optionFour='$optionFour'  WHERE questionNumber=$questionNumber";
	if(mysql_query($q))
		{
			header( "Location: ../add-update.php?isUpdated=yes&test_type=$test_type&action=update#$questionNumber" );
		}
		else
		{
			header( "Location: ../add-update.php?isUpdated=no&action=update" );
		}
		
	}
	elseif(isset($_GET['questionId']) && isset($_GET['test_type']))
	{
		if(mysql_query("DELETE FROM `$questionTable` WHERE questionNumber = '$_GET[questionId]'"))
		{
			$test_type = $_GET['test_type'];
			$url="../add-update.php?isDeleted=yes&test_type=$test_type&action=add";					
			header("Refresh:0;URL=$url");
		}
	}

		
?>
